package com.laomou.poker.rule.landlord;

import java.util.List;
import java.util.Random;

import com.laomou.poker.Poker;
import com.laomou.poker.PokerGroup;
import com.laomou.poker.main.Debug;
import com.laomou.poker.main.Log;
import com.laomou.poker.player.AbsPlayer;
import com.laomou.poker.rule.PokerRuleType;
import com.laomou.poker.table.Table;

public class LandlordTable extends Table {

    public LandlordTable() {
        super(PokerRuleType.斗地主);
    }

    @Override
    public void onPreDealPoker() {
    }

    @Override
    public void onPrePoker() {
        Random r = new java.util.Random();
        int size = getPlayer().size();
        int n = r.nextInt(size);
        for (int i = 0; i < size; i++) {
            if (i == n) {
                setStart(i);
                getPlayer(i).setPlayerType(LandlordType.地主);
            } else {
                getPlayer(i).setPlayerType(LandlordType.平民);
            }
        }
        boolean exit = false;
        int count = 0;
        while (!exit) {
            for (int i = 0; i < size; i++) {
                if (count == 3) {
                    exit = true;
                    setRunError(true);
                    break;
                }
                if (getPlayer(i).isRobFirst()) {
                    setStart(i);
                    exit = true;
                    break;
                } else {
                    Log.print(getPlayer(i).getPlayerName() + " 放弃了地主");
                }
                count++;
            }
        }
        if (!getRunError()) {
            int start = getStart();
            Log.print(getPlayer(start).getPlayerName() + " 抢到了地主");
            PokerGroup pg = new PokerGroup();
            for (Poker poker : getPushPoker()) {
                getTableAllPoker().add(poker);
                pg.addPoker(poker);
            }
            getPlayer(start).addPoker(pg);
            Log.print("底牌 " + getPushPoker());
        }
    }

    /**
     * 判断是否大于参数(poker)的牌
     *
     * @param poker
     * @return true 有 false 没有
     */
    public final boolean hasBigPoker(List<Poker> list, PokerGroup pg) {
        PokerGroup playerPg = new PokerGroup();
        playerPg.addPokers(list);
        return mRule.hasBigPoker(playerPg, pg, null);
    }

    @Override
    public void onStart() {
        int j = getStart();
        boolean hasWin = false;
        while (!hasWin) {
            for (int i = 0; i < getPlayer().size(); i++) {
                if (j == i) {
                    mTableNewPoker = null;
                }
                AbsPlayer player = getPlayer(i);
                if (mTableNewPoker == null) {
                    do {
                        mTableNewPoker = player.getFirstDiscardPoker();
                    } while (!mRule.isRule(mTableNewPoker));
                    Log.print(player.getPlayerName() + " 新出牌 " + mTableNewPoker);
                    player.removePoker(getTableNewPoker());
                    addTablePoker(getTableNewPoker());
                    hasWin = player.win();
                    j = i;
                    continue;
                }
                if (hasBigPoker(player.getPlayerPoker(), mTableNewPoker)) {
                    PokerGroup pg;
                    do {
                        pg = player.getDiscardPoker(getPlayer(j).getPlayerType(), mTableNewPoker);
                        if (pg == null) {
                            break;
                        }
                    } while (!(mRule.isRule(pg) && mRule.isGreater(pg, mTableNewPoker)));
                    if (pg == null) {
                        Log.print(player.getPlayerName() + " 放弃了出牌 ");
                        continue;
                    }
                    mTableNewPoker = pg;
                    player.removePoker(mTableNewPoker);
                    addTablePoker(mTableNewPoker);
                    hasWin = player.win();
                    j = i;
                    Log.print(player.getPlayerName() + " 出牌 " + mTableNewPoker);
                } else {
                    if (Debug.CAN_TIP_NO_BIG_POKER) {
                        Log.print(player.getPlayerName() + " 没有牌大过 "
                                + getPlayer(j).getPlayerName() + " 的 "
                                + mTableNewPoker);
                    }
                }
            }
        }
        Log.print(getPlayer(j).getPlayerName() + "赢");
    }
}
